package com.lianniu.web.demand.controller;

import com.lianniu.web.base.BaseResult;
import com.lianniu.web.buyerOrder.service.IBuyerOrderService;
import com.lianniu.web.buyerProduct.model.BuyerInfoVo;
import com.lianniu.web.buyerProduct.service.IBuyerInfoService;
import com.lianniu.web.demand.service.DemandDetailService;
import com.lianniu.web.demand.service.DemandService;
import com.lianniu.web.demand.service.DemandSupplyService;
import com.lianniu.web.demand.vo.DemandDetailVo;
import com.lianniu.web.demand.vo.DemandSupplyVo;
import com.lianniu.web.demand.vo.DemandVo;
import com.lianniu.web.operationMessage.service.OperMessageService;
import com.lianniu.web.operationMessage.vo.OperMessage;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * Created by Administrator on 2017-05-14.
 */
@Controller
@RequestMapping("/rest/buyer/demand")
public class DemandRestController {
    @Autowired
    private DemandService demandService;
    @Autowired
    private DemandDetailService demandDetailService;
    @Autowired
    private DemandSupplyService demandSupplyService;

    @Autowired
    IBuyerOrderService buyerOrderService;
    @Autowired
    private IBuyerInfoService buyerInfoService;

    @Autowired
    private OperMessageService operMessageService;

    @RequestMapping("/isAuthed")
    @ResponseBody
    public BaseResult isAuthed(String userId) {
        return demandService.isAuthed(userId);
    }

    @RequestMapping("/submitAuth")
    @ResponseBody
    public BaseResult submitAuth(BuyerInfoVo buyerInfoVo) {
        buyerInfoService.updateBuyerAuth(buyerInfoVo);
        try {
            OperMessage operMessage = new OperMessage();
            operMessage.setCreatetime(new Date());
            operMessage.setDelflag("1");
            operMessage.setStatus("1");
            operMessage.setUserid(Long.valueOf(buyerInfoVo.getUserId()));
            operMessage.setType(OperMessage.TYPE_BUYER_AUTHENTICATION);
            operMessage.setSource(OperMessage.RESOURCE_BUYER);
            operMessage.setIsread("1");
            operMessageService.insertSelective(operMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return BaseResult.success();
    }

    @RequestMapping("/publicDemand")
    @ResponseBody
    public BaseResult publicDemand(DemandVo demandVo, DemandDetailVo demandDetailVo) {
        demandVo.setDemandcode((String) buyerOrderService.getCode("DE", 6).getData());
        demandVo.setAddtime(new Date());
        demandVo.setDelflag("1");
        demandVo.setAdduser(demandVo.getUsersid().toString());
        demandVo.setDemandstatus(Byte.valueOf(DemandVo.DEMAND_STATUS_INIT));
        demandVo.setApprovestatus(Byte.valueOf(DemandVo.APPROVE_STATUS_INIT));
        demandVo.setStatus(DemandVo.DEMAND_STATUS_INIT);
        demandVo.setIsdead("1");
        demandVo.setIsonline("1");
        demandService.insertSelective(demandVo);
        demandDetailVo.setDelflag("1");
        demandDetailVo.setAddtime(new Date());
        demandDetailVo.setDemandid(demandVo.getId());
        demandDetailVo.setAdduser(demandVo.getUsersid().toString());
        demandDetailVo.setStatus(DemandDetailVo.STATUS_1);
        demandDetailService.insertSelective(demandDetailVo);
        try {
            OperMessage operMessage = new OperMessage();
            operMessage.setCreatetime(new Date());
            operMessage.setDelflag("1");
            operMessage.setStatus("1");
            operMessage.setUserid(demandVo.getUsersid());
            operMessage.setOrderid(demandVo.getId());
            operMessage.setType(OperMessage.TYPE_DEMAND);
            operMessage.setSource(OperMessage.RESOURCE_BUYER);
            operMessage.setIsread("1");
            operMessageService.insertSelective(operMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return BaseResult.success();
    }

    @RequestMapping("/source")
    @ResponseBody
    public BaseResult source(String pageNum,String userId) {
        int page = 0;
        if (StringUtils.isNotBlank(pageNum)) {
            page = Integer.valueOf(pageNum);
        }

        return this.demandService.findSource(userId, page);
    }

    @RequestMapping("/mine")
    @ResponseBody
    public BaseResult mine(String userId, String pageNum) {
        int page = 0;
        if (!StringUtils.isBlank(pageNum)) {
            page = Integer.valueOf(pageNum);
        }

        return this.demandService.selectByUserId(Long.valueOf(userId), page);
    }

    @RequestMapping("/supplys")
    @ResponseBody
    public BaseResult supplys(String userId, String pageNum) {
        int page = 0;
        if (!StringUtils.isBlank(pageNum)) {
            page = Integer.valueOf(pageNum);
        }

        return this.demandSupplyService.selectByUserId(userId, page);
    }

    @RequestMapping("/demandDetail")
    @ResponseBody
    public BaseResult demandDetail(String id) {
        this.demandService.increaseClickCount(Long.valueOf(id));
        BaseResult baseResult = this.demandService.selectByPrimaryKey(Long.valueOf(id));
        return baseResult;
    }

    @RequestMapping("/demandSupply")
    @ResponseBody
    public BaseResult demandSupply(DemandSupplyVo demandSupplyVo) {
        this.demandService.increaseSupplyCount(demandSupplyVo.getDemandid());
        demandSupplyVo.setDelflag("1");
        demandSupplyVo.setStatus(DemandSupplyVo.STATUS_NORMAL);
        demandSupplyVo.setAddtime(new Date());
        demandSupplyVo.setAdduser(demandSupplyVo.getUserid().toString());
        this.demandSupplyService.insertSelective(demandSupplyVo);

        return BaseResult.success();
    }
}

